"use strict";
cc._RF.push(module, '643ceIral5IeroSlWeR1ZIV', 'AI_node');
// scripts/AI_node.js

"use strict";

var e = require;
var t = module;
var o = exports;

var _ii,
    nn = void 0 && (void 0).__extends || (_ii = function ii(t, e) {
  return (_ii = Object.setPrototypeOf || {
    __proto__: []
  } instanceof Array && function (t, e) {
    t.__proto__ = e;
  } || function (t, e) {
    for (var o in e) {
      Object.prototype.hasOwnProperty.call(e, o) && (t[o] = e[o]);
    }
  })(t, e);
}, function (t, e) {
  function o() {
    this.constructor = t;
  }

  _ii(t, e), t.prototype = null === e ? Object.create(e) : (o.prototype = e.prototype, new o());
}),
    rr = void 0 && (void 0).__awaiter || function (t, e, o, i) {
  return new (o || (o = Promise))(function (n, a) {
    function r(t) {
      try {
        l(i.next(t));
      } catch (e) {
        a(e);
      }
    }

    function s(t) {
      try {
        l(i["throw"](t));
      } catch (e) {
        a(e);
      }
    }

    function l(t) {
      var e;
      t.done ? n(t.value) : (e = t.value, e instanceof o ? e : new o(function (t) {
        t(e);
      })).then(r, s);
    }

    l((i = i.apply(t, e || [])).next());
  });
};

var oo = void 0 && (void 0).__decorate || function (e, t, a, n) {
  var i,
      o = arguments.length,
      s = o < 3 ? t : null === n ? n = Object.getOwnPropertyDescriptor(t, a) : n;
  if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) s = Reflect.decorate(e, t, a, n);else for (var r = e.length - 1; r >= 0; r--) {
    (i = e[r]) && (s = (o < 3 ? i(s) : o > 3 ? i(t, a, s) : i(t, a)) || s);
  }
  return o > 3 && s && Object.defineProperty(t, a, s), s;
};

Object.defineProperty(o, "__esModule", {
  value: !0
});

var a = e("BaseModuleUI"),
    n = e("GlobalParams"),
    i = e("CommonHelper"),
    r = e("PureHelper"),
    s = e("Game"),
    l = e("ObjectPool"),
    _ = e("node_team"),
    c = e("role_effects_info"),
    d = e("role_node"),
    h = e("role_skill"),
    u = e("tower_node_info"),
    p = cc._decorator,
    m = p.ccclass,
    f = (p.property, function (e) {
  function t() {
    var t = null !== e && e.apply(this, arguments) || this;
    return t.role_node = null, t.team_info = s.TeamInfo.rest_team, t.ai_disabled = !1, t.ai_addHp = !1, t.home_pos = null, t.ai_attack = !1, t.add_heal = null, t.ack_range = 0, t.is_map_traget = !1, t.traget_pos = cc.v2(0, 0), t.move_tower_range = !1, t.to_tower_range = cc.v2(0, 0), t.next_index = 0, t.road_index = 0, t.target_list = [], t.target_node = null, t;
  }

  return nn(t, e), t.prototype.initView = function () {
    this.is_map_traget = !1, this.home_pos = this.node.getPosition(), this.role_node = this.node.getComponent(d["default"]), this.updateTime();
  }, t.prototype.start = function () {
    this.ack_range = this.node.getComponent(h["default"]).ack_range;
  }, t.prototype.showAIRoadInfo = function (e) {
    this.team_info = this.node.getComponent(_["default"]).getTeamInfo(), this.next_index = this.team_info == s.TeamInfo.right_team ? 1 : 5, this.target_list = [], this.road_index = e < 4 ? 0 : e < 6 ? 1 : 2;

    for (var t = n.GlobalParams.GameDestroyTower[this.team_info == s.TeamInfo.left_team ? 0 : 1], o = 0; o < 3; o++) {
      o != this.road_index && (this.road_index = t[o] < t[this.road_index] ? o : this.road_index);
    }
  }, t.prototype.updateTime = function () {
    var e = this;
    if (n.GlobalParams.GameStart) if (this.is_map_traget) {
      if (r["default"].Distance(this.node.getPosition(), this.traget_pos) < 50) this.is_map_traget = !1;else {
        var t = cc.v2(this.traget_pos.x, this.traget_pos.y).sub(this.node.getPosition()).normalize();
        this.role_node.roleMoveInfo(t);
      }
    } else this.role_node.role_death || (this.ai_disabled ? this.goHomeAddHp() : this.role_node.getRoleHpPercent() < 30 && !n.GlobalParams.PlayGuideMode && 2 != n.GlobalParams.MultiModeIndex ? (this.updateNextIndex(this.team_info == s.TeamInfo.left_team), this.ai_disabled = !0) : this.move_tower_range ? this.moveToTowerRange() : (this.ai_addHp = !1, this.getAttackTarget()));
    this.scheduleOnce(function () {
      e.updateTime();
    }, n.GlobalParams.GameUpdateTime);
  }, t.prototype.getAttackTarget = function () {
    var e = 9999,
        t = cc.v2(0, 0);
    if (this.target_list.length < 1) this.target_node = null, t = s["default"].game_path_pos[this.road_index][this.next_index];else {
      this.target_node = null;

      for (var o = 0; o < this.target_list.length; o++) {
        var a = this.target_list[o];
        a.active && e > (i = Math.abs(r["default"].Distance(this.node.getPosition(), a.getPosition()))) && (e = i, this.target_node = a);
      }
    }
    this.ai_attack || null == this.target_node || (this.node.scaleX = this.node.x > this.target_node.x ? 1 : -1), this.ai_attack = !1;
    var i,
        l = new cc.Vec2(0, 0);
    null == this.target_node ? (l = cc.v2(t.x, t.y).sub(cc.v2(this.node.x, this.node.y)).normalize(), this.role_node.roleMoveInfo(l), (i = Math.abs(r["default"].Distance(this.node.getPosition(), t))) < 100 && this.updateNextIndex(this.team_info == s.TeamInfo.right_team)) : (l = cc.v2(this.target_node.x, this.target_node.y).sub(cc.v2(this.node.x, this.node.y)).normalize(), "tower" == this.target_node.group && null == this.target_node.getComponent(u["default"]).getTowerTargetInfo() && e < 500 ? this.role_node.roleStopMove() : e > this.ack_range ? this.role_node.roleMoveInfo(l) : Math.abs(this.target_node.y - this.node.y) > 80 ? this.role_node.roleMoveInfo(l) : (this.role_node.roleStopMove(), this.ai_attack = !0, 2 == n.GlobalParams.MultiModeIndex ? this.role_node.AISpecialSkill() : "role" == this.target_node.group ? this.role_node.AISpecialSkill() : this.role_node.roleCommonSkill()));
  }, t.prototype.goHomeAddHp = function () {
    var e = Math.abs(r["default"].Distance(this.node.getPosition(), this.home_pos));
    if (this.ai_addHp) this.role_node.setRoleAddHp(), this.role_node.getRoleHpPercent() > 75 && (this.ai_disabled = !1, this.add_heal.destroy());else if (e <= 50) {
      this.role_node.roleStopMove();
      var t = l["default"].getInstance().creatPlane(s.Pool_Type.role_effects);
      this.add_heal = t, t.setParent(this.node), t.getComponent(c["default"]).showEffectInfo(s.RoleEffectType.home_heal), this.ai_addHp = !0;
    } else {
      var o = cc.v2(0, 0);
      o = this.next_index > 0 && this.next_index < 6 ? s["default"].game_path_pos[this.road_index][this.next_index] : this.home_pos;
      var a = cc.v2(o.x, o.y).sub(this.node.getPosition()).normalize();
      this.role_node.roleMoveInfo(a), Math.abs(r["default"].Distance(this.node.getPosition(), o)) < 100 && this.updateNextIndex(this.team_info == s.TeamInfo.left_team), this.role_node.getRoleHpPercent() > 75 && (this.updateNextIndex(this.team_info == s.TeamInfo.right_team), this.ai_disabled = !1);
    }
  }, t.prototype.updateNextIndex = function (e) {
    e ? this.next_index++ : this.next_index--, this.next_index = this.next_index > 6 ? 6 : this.next_index, this.next_index = this.next_index < 0 ? 0 : this.next_index;
  }, t.prototype.onCollisionEnter = function (e, t) {
    var o = e.node,
        a = o.group;

    if (o.isValid && (50 == t.tag && 0 == e.tag && this.getSoliderInfo(o) && this.target_list.push(o), "map" == a && 0 == t.tag && !this.is_map_traget && 99 != e.tag && 88 != e.tag)) {
      this.is_map_traget = !0;
      var n = o.children[e.tag].getPosition();
      n = o.convertToWorldSpaceAR(n), n = this.node.parent.convertToNodeSpaceAR(n), this.traget_pos = n;
    }
  }, t.prototype.onCollisionExit = function (e, t) {
    var o = e.node;

    if (o.group, o.isValid && 50 == t.tag && 0 == e.tag && this.getSoliderInfo(o)) {
      var a = this.target_list.indexOf(o);
      this.target_list.splice(a, 1);
    }
  }, t.prototype.getSoliderInfo = function (e) {
    var t = e.name;
    return t == "soldier_team_" + (this.team_info == s.TeamInfo.left_team ? 2 : 1) || "role_team" == t.substring(0, 9) && this.team_info % 2 == Number(i["default"].getNameType(t, "role_team_")) % 2 || "tower" == t.substring(0, 5) && this.team_info != e.getComponent(_["default"]).getTeamInfo();
  }, t.prototype.moveTowerRange = function () {
    this.move_tower_range = !0, this.to_tower_range.x = this.node.x + (this.role_node.match_game_index % 2 == 0 ? -400 : 400), this.to_tower_range.y = this.node.y;
  }, t.prototype.moveToTowerRange = function () {
    if (Math.abs(r["default"].Distance(this.node.getPosition(), this.to_tower_range)) <= 50) this.role_node.roleStopMove(), this.move_tower_range = !1, this.to_tower_range = cc.v2(0, 0);else {
      var e = cc.v2(this.to_tower_range.x, this.to_tower_range.y).sub(this.node.getPosition()).normalize();
      this.role_node.roleMoveInfo(e);
    }
  }, oo([m], t);
}(a["default"]));

o["default"] = f;

cc._RF.pop();